const date = new Date()
const years = []
const year = date.getFullYear()
const months = []
const month = date.getMonth() + 1
const days = []
const day = date.getDate()
for (let i = 1990; i <= date.getFullYear(); i++) {
  years.push(i)
}
for (let i = 1; i <= 12; i++) {
  months.push(i)
}
for (let i = 1; i <= 31; i++) {
  days.push(i)
}
export default {
  data() {
    return {
      title: 'picker-view',
      years,
      year,
      months,
      month,
      days,
      day,
      value: [9999, month - 1, day - 1],
      indicatorStyle: `height: ${Math.round(uni.getSystemInfoSync().screenWidth/(750/100))}px;`,
      show: false,
      isMonth: true,
      dateValue: '选择时间',
      begintime: '选择时间',
      endtime: '选择时间',
      value1: '起始时间',
      value2: '终止时间',
      beginvalue1: '起始时间',
      endvalue2: '终止时间',
      curr: 0
    }
  },
  methods: {
    bindChange(e) {
      const val = e.detail.value
      this.year = this.years[val[0]]
      this.month = this.months[val[1]] > 9 ? this.months[val[1]] : '0' + this.months[val[1]]
      this.day = this.days[val[2]] > 9 ? this.days[val[2]] : '0' + this.days[val[2]]
      if (this.isMonth) {
        this.dateValue = this.year + '年' + this.month + '月'
        this.begintime = this.year + '-' + this.month + '-01'
        this.endtime = this.year + '-' + this.month + '-' + this.days[this.days.length - 1]
      } else {
        console.log(this.year + '年' + this.month + '月' + this.day + '日')
        if (this.curr === 0) {
          this.value1 = this.year + '年' + this.month + '月' + this.day + '日'
          this.beginvalue1 = this.year + '-' + this.month + '-' + this.day
        } else if (this.curr === 1) {
          this.value2 = this.year + '年' + this.month + '月' + this.day + '日'
          this.endvalue2 = this.year + '-' + this.month + '-' + this.day
        }
      }
    },
    async leave() {
      if (!this.isMonth && this.endvalue2 === '终止时间') {
        uni.showToast({
          title: '请选择终止时间',
          icon: 'none'
        })
        return false
      } else {
        this.$store.commit('user/setTime', {
          beginTime: this.isMonth ? this.begintime : this.beginvalue1,
          endTime: this.isMonth ? this.endtime : this.endvalue2,
        })
        await uni.navigateBack({
          delta: 1
        })
      }

    },
    del() {
      this.$store.state.user.beginTime = ''
      this.$store.state.user.endTime = ''
      uni.navigateBack({
			  delta: 1
      })
    },
    switchType() {
      this.isMonth = !this.isMonth
    },
    choose(idx) {
      this.curr = idx
    }
  }
}
